﻿using Microsoft.ExtendedReflection.Monitoring;
using Microsoft.VisualStudio.TestTools.Common;
using Microsoft.VisualStudio.TestTools.Execution;

namespace MolesVS2012.Host.Host
{
    internal sealed class SafeExecutionRunConfiguration : IExecutionRunConfiguration
    {
        private readonly IExecutionRunConfiguration executionRunConfiguration;
        private ITestElement[] _testElements;
        private TestRun _testRun;

        public ITestElement[] TestElements
        {
            get
            {
                if (this._testElements == null)
                {
                    using (_ProtectingThreadContext.Acquire())
                        this._testElements = this.executionRunConfiguration.TestElements;
                }
                return this._testElements;
            }
        }

        public TestRun TestRun
        {
            get
            {
                if (this._testRun == null)
                {
                    using (_ProtectingThreadContext.Acquire())
                        this._testRun = this.executionRunConfiguration.TestRun;
                }
                return this._testRun;
            }
        }

        public SafeExecutionRunConfiguration(IExecutionRunConfiguration executionRunConfiguration)
        {
            this.executionRunConfiguration = executionRunConfiguration;
        }
    }
}
